Imports cmplib01


'// What is the purpose of this control?

Public Class ctlBattleCardView
    Inherits System.Windows.Forms.UserControl

#Region " Windows Form Designer generated code "

    Public Sub New()
        MyBase.New()

        'This call is required by the Windows Form Designer.
        InitializeComponent()

        'Add any initialization after the InitializeComponent() call

    End Sub

    'UserControl overrides dispose to clean up the component list.
    Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
        If disposing Then
            If Not (components Is Nothing) Then
                components.Dispose()
            End If
        End If
        MyBase.Dispose(disposing)
    End Sub

    'Required by the Windows Form Designer
    Private components As System.ComponentModel.IContainer

    'NOTE: The following procedure is required by the Windows Form Designer
    'It can be modified using the Windows Form Designer.  
    'Do not modify it using the code editor.
    Friend WithEvents pnlMain As System.Windows.Forms.Panel
    Friend WithEvents cboCombats As ComboBox 'LumiSoft.UI.Controls.WComboBox
    Friend WithEvents pnlToolbar As System.Windows.Forms.Panel
    Friend WithEvents tlbShow As System.Windows.Forms.ToolBar
    Friend WithEvents btnAttackingUnits As System.Windows.Forms.ToolBarButton
    Friend WithEvents btnReinforcingUnits As System.Windows.Forms.ToolBarButton
    Friend WithEvents imlToolButs As System.Windows.Forms.ImageList
    Friend WithEvents btnDefendingUnits As System.Windows.Forms.ToolBarButton
    Friend WithEvents btnDefendingReinforcement As System.Windows.Forms.ToolBarButton
    Friend WithEvents btnDefendingInstallation As System.Windows.Forms.ToolBarButton
    Friend WithEvents btnAttackingFaction As System.Windows.Forms.ToolBarButton
    Friend WithEvents btnDefendingFaction As System.Windows.Forms.ToolBarButton
    Friend WithEvents btnAttackerRules As System.Windows.Forms.ToolBarButton
    Friend WithEvents btnDefenderRules As System.Windows.Forms.ToolBarButton
    Friend WithEvents txtDescription As System.Windows.Forms.TextBox
    Friend WithEvents pnlTerrain As System.Windows.Forms.Panel
    Friend WithEvents lblTerrain As System.Windows.Forms.LinkLabel
    Friend WithEvents Panel1 As System.Windows.Forms.Panel
    Friend WithEvents lvwData As System.Windows.Forms.ListView
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
        Me.components = New System.ComponentModel.Container
        Dim resources As System.Resources.ResourceManager = New System.Resources.ResourceManager(GetType(ctlBattleCardView))
        Me.pnlMain = New System.Windows.Forms.Panel
        Me.txtDescription = New System.Windows.Forms.TextBox
        Me.lvwData = New System.Windows.Forms.ListView
        Me.pnlToolbar = New System.Windows.Forms.Panel
        Me.tlbShow = New System.Windows.Forms.ToolBar
        Me.btnAttackingFaction = New System.Windows.Forms.ToolBarButton
        Me.btnAttackingUnits = New System.Windows.Forms.ToolBarButton
        Me.btnReinforcingUnits = New System.Windows.Forms.ToolBarButton
        Me.btnAttackerRules = New System.Windows.Forms.ToolBarButton
        Me.btnDefendingFaction = New System.Windows.Forms.ToolBarButton
        Me.btnDefendingUnits = New System.Windows.Forms.ToolBarButton
        Me.btnDefendingReinforcement = New System.Windows.Forms.ToolBarButton
        Me.btnDefendingInstallation = New System.Windows.Forms.ToolBarButton
        Me.btnDefenderRules = New System.Windows.Forms.ToolBarButton
        Me.imlToolButs = New System.Windows.Forms.ImageList(Me.components)
        Me.cboCombats = New ComboBox 'LumiSoft.UI.Controls.WComboBox
        Me.pnlTerrain = New System.Windows.Forms.Panel
        Me.lblTerrain = New System.Windows.Forms.LinkLabel
        Me.Panel1 = New System.Windows.Forms.Panel
        Me.pnlMain.SuspendLayout()
        Me.pnlToolbar.SuspendLayout()
        Me.pnlTerrain.SuspendLayout()
        Me.Panel1.SuspendLayout()
        Me.SuspendLayout()
        '
        'pnlMain
        '
        Me.pnlMain.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
                    Or System.Windows.Forms.AnchorStyles.Left), System.Windows.Forms.AnchorStyles)
        Me.pnlMain.BackColor = System.Drawing.Color.LightSteelBlue
        Me.pnlMain.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.pnlMain.Controls.Add(Me.txtDescription)
        Me.pnlMain.Controls.Add(Me.lvwData)
        Me.pnlMain.Controls.Add(Me.pnlToolbar)
        Me.pnlMain.DockPadding.All = 5
        Me.pnlMain.Location = New System.Drawing.Point(8, 80)
        Me.pnlMain.Name = "pnlMain"
        Me.pnlMain.Size = New System.Drawing.Size(224, 232)
        Me.pnlMain.TabIndex = 0
        '
        'txtDescription
        '
        Me.txtDescription.Anchor = CType((System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left), System.Windows.Forms.AnchorStyles)
        Me.txtDescription.BackColor = System.Drawing.Color.Gainsboro
        Me.txtDescription.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.txtDescription.Location = New System.Drawing.Point(8, 160)
        Me.txtDescription.Multiline = True
        Me.txtDescription.Name = "txtDescription"
        Me.txtDescription.ReadOnly = True
        Me.txtDescription.ScrollBars = System.Windows.Forms.ScrollBars.Horizontal
        Me.txtDescription.Size = New System.Drawing.Size(208, 64)
        Me.txtDescription.TabIndex = 4
        Me.txtDescription.Text = "Selected Item Text"
        '
        'lvwData
        '
        Me.lvwData.Anchor = CType(((System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom) _
                    Or System.Windows.Forms.AnchorStyles.Left), System.Windows.Forms.AnchorStyles)
        Me.lvwData.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
        Me.lvwData.Location = New System.Drawing.Point(8, 32)
        Me.lvwData.Name = "lvwData"
        Me.lvwData.Size = New System.Drawing.Size(208, 120)
        Me.lvwData.TabIndex = 3
        '
        'pnlToolbar
        '
        Me.pnlToolbar.BackColor = System.Drawing.Color.SteelBlue
        Me.pnlToolbar.Controls.Add(Me.tlbShow)
        Me.pnlToolbar.Location = New System.Drawing.Point(0, 0)
        Me.pnlToolbar.Name = "pnlToolbar"
        Me.pnlToolbar.Size = New System.Drawing.Size(224, 24)
        Me.pnlToolbar.TabIndex = 2
        '
        'tlbShow
        '
        Me.tlbShow.Appearance = System.Windows.Forms.ToolBarAppearance.Flat
        Me.tlbShow.Buttons.AddRange(New System.Windows.Forms.ToolBarButton() {Me.btnAttackingFaction, Me.btnAttackingUnits, Me.btnReinforcingUnits, Me.btnAttackerRules, Me.btnDefendingFaction, Me.btnDefendingUnits, Me.btnDefendingReinforcement, Me.btnDefendingInstallation, Me.btnDefenderRules})
        Me.tlbShow.DropDownArrows = True
        Me.tlbShow.ImageList = Me.imlToolButs
        Me.tlbShow.Location = New System.Drawing.Point(0, 0)
        Me.tlbShow.Name = "tlbShow"
        Me.tlbShow.ShowToolTips = True
        Me.tlbShow.Size = New System.Drawing.Size(224, 28)
        Me.tlbShow.TabIndex = 0
        '
        'btnAttackingFaction
        '
        Me.btnAttackingFaction.ImageIndex = 0
        Me.btnAttackingFaction.Tag = "btnAttackingFaction"
        '
        'btnAttackingUnits
        '
        Me.btnAttackingUnits.ImageIndex = 1
        Me.btnAttackingUnits.Tag = "btnAttackingUnits"
        '
        'btnReinforcingUnits
        '
        Me.btnReinforcingUnits.ImageIndex = 2
        Me.btnReinforcingUnits.Tag = "btnReinforcingUnits"
        Me.btnReinforcingUnits.Visible = False
        '
        'btnAttackerRules
        '
        Me.btnAttackerRules.ImageIndex = 3
        Me.btnAttackerRules.Tag = "btnAttackerRules"
        '
        'btnDefendingFaction
        '
        Me.btnDefendingFaction.ImageIndex = 4
        Me.btnDefendingFaction.Tag = "btnDefendingFaction"
        '
        'btnDefendingUnits
        '
        Me.btnDefendingUnits.ImageIndex = 5
        Me.btnDefendingUnits.Tag = "btnDefendingUnits"
        '
        'btnDefendingReinforcement
        '
        Me.btnDefendingReinforcement.ImageIndex = 6
        Me.btnDefendingReinforcement.Tag = "btnDefendingReinforcement"
        Me.btnDefendingReinforcement.Visible = False
        '
        'btnDefendingInstallation
        '
        Me.btnDefendingInstallation.ImageIndex = 7
        Me.btnDefendingInstallation.Tag = "btnDefendingInstallation"
        '
        'btnDefenderRules
        '
        Me.btnDefenderRules.ImageIndex = 8
        Me.btnDefenderRules.Tag = "btnDefenderRules"
        '
        'imlToolButs
        '
        Me.imlToolButs.ImageSize = New System.Drawing.Size(16, 16)
        Me.imlToolButs.ImageStream = CType(resources.GetObject("imlToolButs.ImageStream"), System.Windows.Forms.ImageListStreamer)
        Me.imlToolButs.TransparentColor = System.Drawing.Color.Transparent
        '
        'cboCombats
        '
        '        Me.cboCombats.DrawBorder = True
        Me.cboCombats.DropDownWidth = 208
        Me.cboCombats.Location = New System.Drawing.Point(8, 8)
        Me.cboCombats.Name = "cboCombats"
        Me.cboCombats.SelectedIndex = -1
        'Me.cboCombats.SelectionLength = 0
        'Me.cboCombats.SelectionStart = 0
        Me.cboCombats.Size = New System.Drawing.Size(208, 20)
        Me.cboCombats.TabIndex = 0
        Me.cboCombats.Text = "Blackgutz vs Ironshore"
        '        Me.cboCombats.UseStaticViewStyle = True
        '
        'cboCombats.ViewStyle
        '
        '       Me.cboCombats.VisibleItems = 10
        '
        'pnlTerrain
        '
        Me.pnlTerrain.BackColor = System.Drawing.Color.LightSteelBlue
        Me.pnlTerrain.Controls.Add(Me.lblTerrain)
        Me.pnlTerrain.Location = New System.Drawing.Point(8, 44)
        Me.pnlTerrain.Name = "pnlTerrain"
        Me.pnlTerrain.Size = New System.Drawing.Size(224, 32)
        Me.pnlTerrain.TabIndex = 2
        '
        'lblTerrain
        '
        Me.lblTerrain.Location = New System.Drawing.Point(8, 8)
        Me.lblTerrain.Name = "lblTerrain"
        Me.lblTerrain.Size = New System.Drawing.Size(208, 16)
        Me.lblTerrain.TabIndex = 0
        Me.lblTerrain.TabStop = True
        Me.lblTerrain.Text = "Plains and Forest"
        '
        'Panel1
        '
        Me.Panel1.BackColor = System.Drawing.Color.LightSteelBlue
        Me.Panel1.Controls.Add(Me.cboCombats)
        Me.Panel1.Location = New System.Drawing.Point(8, 8)
        Me.Panel1.Name = "Panel1"
        Me.Panel1.Size = New System.Drawing.Size(224, 32)
        Me.Panel1.TabIndex = 3
        '
        'ctlBattleCardView
        '
        Me.BackColor = System.Drawing.Color.Black
        Me.Controls.Add(Me.Panel1)
        Me.Controls.Add(Me.pnlMain)
        Me.Controls.Add(Me.pnlTerrain)
        Me.Name = "ctlBattleCardView"
        Me.Size = New System.Drawing.Size(240, 320)
        Me.pnlMain.ResumeLayout(False)
        Me.pnlToolbar.ResumeLayout(False)
        Me.pnlTerrain.ResumeLayout(False)
        Me.Panel1.ResumeLayout(False)
        Me.ResumeLayout(False)

    End Sub

#End Region

    '// The code in this control sucks ballz.

    Private Enum DisplayMode
        AttackingFaction = 1
        AttackingUnits = 2
        ReinforcingUnits = 3
        AttackerRules = 4
        DefendingFaction = 5
        DefendingUnits = 6
        DefendingInstallation = 7
        DefenderRules = 8
        DefendingReinforcingUnits = 9
    End Enum

    Public Event BattleSelected(ByRef batt As Battle)

    Private ptrGameData As CampaignerGameData
    Private m_SelectedBattle As Battle
    Private m_DisplayMode As DisplayMode

#Region "properties"
    Public Property GameData() As CampaignerGameData
        Get
            Return ptrGameData
        End Get
        Set(ByVal Value As CampaignerGameData)
            ptrGameData = Value
            ListBattles()
        End Set
    End Property

    Public Property SelectedBattle() As Battle
        Get
            Return m_SelectedBattle
        End Get
        Set(ByVal Value As Battle)
            m_SelectedBattle = Value
            Me.lvwData.Items.Clear()
            If Not m_SelectedBattle Is Nothing Then RaiseEvent BattleSelected(m_SelectedBattle)
        End Set
    End Property

    Private Property ItemDisplayMode() As DisplayMode
        Get
            Return m_DisplayMode
        End Get
        Set(ByVal Value As DisplayMode)
            m_DisplayMode = Value
            DisplayValues()
        End Set
    End Property

#End Region

    Private Sub ListBattles()
        Try
            ClearAll()

            If ptrGameData Is Nothing Then Exit Sub

            '// lets create each battle and populate it, then ensure all nodes invisible.
            Dim Batt As Battle
            Dim battKey As String
            For Each battKey In ptrGameData.Battles_g.HT.Keys
                Batt = ptrGameData.Battles_g.Item(battKey)
                cboCombats.Items.Add(Batt.BattleName)
            Next
            cboCombats.SelectedIndex = 0

        Catch ex As Exception
            HandleException("ctlCombatsTreeview.ListBattles", ex)
        End Try
    End Sub

    Private Sub ClearAll()
        cboCombats.Items.Clear()
        lvwData.Items.Clear()
        txtDescription.Text = "Selected Item Text"

    End Sub

    Private Sub cboCombats_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboCombats.SelectedIndexChanged

        Try
            If cboCombats.SelectedItem Is Nothing Then Exit Sub
            'Dim wCombo As LumiSoft.UI.Controls.WComboItem = cboCombats.SelectedItem

            SelectedBattle = cboCombats.SelectedItem 'wCombo.Tag

        Catch ex As Exception
            HandleException("ctlBattleCardView.cboCombats.SelectedIndexChanged", ex)
        End Try

    End Sub

    Private Sub tlbShow_ButtonClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.ToolBarButtonClickEventArgs) Handles tlbShow.ButtonClick
        If SelectedBattle Is Nothing Then Exit Sub

        Select Case e.Button.Tag
            Case "btnAttackingFaction" : ItemDisplayMode = DisplayMode.AttackingFaction
            Case "btnAttackingUnits" : ItemDisplayMode = DisplayMode.AttackingUnits
            Case "btnReinforcingUnits" : ItemDisplayMode = DisplayMode.ReinforcingUnits
            Case "btnAttackerRules" : ItemDisplayMode = DisplayMode.AttackerRules
            Case "btnDefendingFaction" : ItemDisplayMode = DisplayMode.DefendingFaction
            Case "btnDefendingUnits" : ItemDisplayMode = DisplayMode.DefendingUnits
            Case "btnDefendingInstallation" : ItemDisplayMode = DisplayMode.DefendingInstallation
            Case "btnDefenderRules" : ItemDisplayMode = DisplayMode.DefenderRules
            Case "btnDefendingReinforcement" : ItemDisplayMode = DisplayMode.DefendingReinforcingUnits
        End Select
    End Sub

    Private Sub DisplayValues()
        '// displays whatever the user selected. Yeeehaw.
        Try
            lvwData.Items.Clear()
            Select Case m_DisplayMode
                Case DisplayMode.AttackingFaction : DisplayAttackingFaction()
                Case DisplayMode.ReinforcingUnits : DisplayReinforcingUnits()
                Case DisplayMode.AttackingUnits : DisplayAttackingUnits()
                Case DisplayMode.AttackerRules : DisplayAttackerRules()
                Case DisplayMode.DefendingFaction : DisplayDefendingFaction()
                Case DisplayMode.DefendingUnits : DisplayDefendingUnits()
                Case DisplayMode.DefendingReinforcingUnits : DisplayDefenderReinforcingUnits()
                Case DisplayMode.DefendingFaction : DisplayDefendingFaction()
                Case DisplayMode.DefendingInstallation : DisplayDefendingInstallation()
                Case DisplayMode.DefenderRules : DisplayDefenderRules()
            End Select
        Catch ex As Exception
            HandleException("ctlBattleCardView.DisplayValues", ex)
        End Try
    End Sub

#Region "Stupid Display Crap that should be better"

    Private Sub DisplayAttackingFaction()

        Dim fact As Faction = ptrGameData.Faction_g.Item(CampaignerGameData.CDGKEY_FACTION & m_SelectedBattle.AttackingFactionID)
        DisplayFactionData(fact)

    End Sub

    Private Sub DisplayFactionData(ByRef fact As Faction)

        Dim lvw As New ListViewItem(fact.FactionName)
        lvwData.Columns.Clear()
        lvwData.View = View.List
        lvwData.Items.Add(lvw)

        Dim lvw2 As New ListViewItem(fact.FactionDeployedPoints & " points deployed")
        lvwData.Items.Add(lvw2)

        Dim vict As IVictoryCondition
        Dim maxVicVP As Integer
        Dim achievedVP As Integer

        For Each vict In fact.FactionVictoryConditions
            maxVicVP += vict.VPValue
            If vict.Achieved Then achievedVP += vict.VPValue
        Next

        Dim sVictory As String = "(" & achievedVP & "/" & maxVicVP & ") Victory Points"
        Dim lvwVictory As New ListViewItem(sVictory)
        lvwData.Items.Add(lvwVictory)

    End Sub

    Private Sub DisplayUnits(ByVal FactionID As Integer)
        'Dim chtkey As String
        'Dim gc As GroundChitType

        For Each mem As ChitMember In m_SelectedBattle.InvolvedChits
            If mem.OwningFaction.FactionID = FactionID Then
                DisplayUnit(mem)
            End If
        Next

        'For Each chtkey In m_SelectedBattle.InvolvedChits.Keys
        '    gc = ptrGameData.AllChits.Item(chtkey)
        '    ' catch the nothing (when an installation is in the list)
        '    If Not gc Is Nothing Then
        '        If gc.OwningFaction = FactionID Then
        '            ' display this unit
        '            DisplayUnit(CType(gc, UnitType))
        '        End If
        '    End If
        'Next
    End Sub

    Private Sub DisplayUnit(ByRef unit As BattlegroupChit)
        ' name, pv/maxpv, orders, ...

        ' set up columns
        Dim width As Integer = lvwData.Width / 3
        lvwData.Columns.Clear()
        lvwData.Columns.Add("Unit Name", width, HorizontalAlignment.Center)
        lvwData.Columns.Add("pv/mpv", width, HorizontalAlignment.Center)
        lvwData.Columns.Add("Orders", width, HorizontalAlignment.Center)

        lvwData.View = View.Details

        ' name and pvs
        Dim lvwItem As New ListViewItem(unit.Name)
        Dim subI As New ListViewItem.ListViewSubItem
        subI.Text = unit.CurrentPV & "/" & unit.deployedpointvalue
        lvwItem.SubItems.Add(subI)

        ' orders
        subI = New ListViewItem.ListViewSubItem
        subI.Text = unit.Order
        lvwItem.SubItems.Add(subI)

        lvwData.Items.Add(lvwItem)

    End Sub

    Private Sub DisplayReinforcers(ByVal FactionID As Integer)

    End Sub

    Private Sub DisplayRules(ByVal FactionID As Integer)
        'Dim ruleKey As String
        Dim assgn As AssignedRule
        Dim col As ChitRules_c
        Dim lvwItem As ListViewItem

        lvwData.Columns.Clear()
        lvwData.View = View.List

        If FactionID = m_SelectedBattle.AttackingFactionID Then
            col = m_SelectedBattle.AttackerAssignedRules
        Else
            col = m_SelectedBattle.DefenderAssignedRules
        End If

        For Each assgn In col
            'assgnRule = ht.Item(ruleKey)
            lvwItem = New ListViewItem(assgn.DisplayName)

            lvwData.Items.Add(lvwItem)
        Next

    End Sub

    Private Sub DisplayAttackingUnits()

        DisplayUnits(m_SelectedBattle.AttackingFactionID)

    End Sub

    Private Sub DisplayReinforcingUnits()

    End Sub

    Private Sub DisplayAttackerRules()
        DisplayRules(m_SelectedBattle.AttackingFactionID)
    End Sub

    Private Sub DisplayDefendingFaction()
        Dim fact As Faction = ptrGameData.Faction_g.Item(CampaignerGameData.CDGKEY_FACTION & m_SelectedBattle.DefendingFactionID)
        DisplayFactionData(fact)
    End Sub

    Private Sub DisplayDefendingUnits()

        DisplayUnits(m_SelectedBattle.DefendingFactionID)

    End Sub

    Private Sub DisplayDefendingInstallation()
        '        Dim chtkey As String
        '       Dim gc As GroundChitType


        For Each mem As ChitMember In m_SelectedBattle.InvolvedChits
            If mem.UnitType = GroundChitClass_cgd.Objective Then
                lvwData.Columns.Clear()
                lvwData.View = View.List
                Dim lvw As New ListViewItem(mem.Name)
                lvwData.Items.Add(lvw)
            End If
        Next


        'For Each chtkey In m_SelectedBattle.InvolvedChits.Keys
        '    GC = m_SelectedBattle.InvolvedChits.item(chtkey)
        '    If GC.GroundChitClass = GroundChitClass_cgd.Objective Then
        '        lvwData.Columns.Clear()
        '        lvwData.View = View.List
        '        Dim lvw As New ListViewItem(GC.Name)
        '        lvwData.Items.Add(lvw)
        '    End If
        'Next
    End Sub

    Private Sub DisplayDefenderRules()
        DisplayRules(m_SelectedBattle.DefendingFactionID)
    End Sub

    Private Sub DisplayDefenderReinforcingUnits()

    End Sub

#End Region

End Class
